<template>
  <div class="driver-archive-container">
    <div class="driver-archive-header">
      <!-- <span class="driver-archive-title">驾驶员档案 </span> -->
      <el-form ref="form" :model="DriverArchive" label-width="60px" class="driver-archive-body-header">
        <el-form-item label="姓名:" class="driver-archive-input">
          <el-input v-model="DriverArchive.DriverName" placeholder="请输入姓名" prefix-icon="el-icon-search"
            clearable></el-input>
        </el-form-item>
        <el-form-item label="驾龄:" class="driver-archive-input">
          <el-input v-model="DriverArchive.DrivingAge" placeholder="请输入驾龄" prefix-icon="el-icon-search"
            clearable></el-input>
        </el-form-item>
        <el-form-item label="电话号:" class="driver-archive-input" style="width: 28%;">
          <el-input v-model="DriverArchive.DriverPhone" placeholder="请输入电话号" prefix-icon="el-icon-search"
            clearable></el-input>
        </el-form-item>
        <el-form-item label="准驾车型:" label-width="90px" class="driver-archive-select">
          <el-select v-model="DriverArchive.LicenseType" clearable placeholder="请选择准驾车型"
            class="driver-archive-select-item">
            <el-option v-for="(item, $index) in licenseTypeList" :key="$index" :label="item" :value="item">
            </el-option>
          </el-select>
        </el-form-item>

        <el-button type="primary" class="driver-archive-button">查询</el-button>
        <el-button type="primary" style="margin-left: 0;" class="driver-archive-button"
          @click="ToAddDriverArchives">新建</el-button>
      </el-form>


    </div>

    <div class="driver-archive-body">
      <div class="driver-archive-horizon"></div>

      <div class="driver-archive-table">
        <el-table :data="this.filtedTableData" style="width: 100%">
          <el-table-column prop="userId" label="工号" align="center" min-width="15%">
          </el-table-column>
          <el-table-column prop="driverName" label="姓名" align="center" min-width="10%">
          </el-table-column>
          <el-table-column prop="licenseType" label="准驾车型" align="center" min-width="15%">
          </el-table-column>
          <el-table-column prop="drivingAge" label="驾龄" align="center" min-width="5%">
          </el-table-column>
          <el-table-column prop="driverPhone" label="联系方式" align="center" min-width="15%">
          </el-table-column>
          <el-table-column prop="driverIdNumber" label="身份证号" align="center" min-width="20%">
          </el-table-column>
          <el-table-column prop="driverLicenseValidity" label="年限" align="center" min-width="10%">
          </el-table-column>
          <el-table-column prop="action" label="操作" align="center" min-width="10%">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="checkAndUpdate(scope.row)">详情</el-button>
              <el-button type="text" size="small" style="color: red;" @click="DriverArchiveDelete(scope.row)">删除
              </el-button>
            </template>

          </el-table-column>
        </el-table>
      </div>
      <div class="pagination">
        <el-pagination background layout="total, sizes, prev, pager, next, jumper" :current-page="tablePage.pageNum"
          :page-size="tablePage.pageSize" :page-sizes="pageSizes" :total="tablePage.total" @size-change="handleSizeChange"
          @current-change="handlePageChange" style="padding-right: 30px;" />
      </div>
    </div>

  </div>
</template>

<script>
import { driverPageQuery } from '../../api/archives'
import { driverArchiveDelete } from '../../api/archives'

export default {
  name: "DriverArchives",
  data() {
    return {
      tableData: [],
      form: {
        UserId: null,
        DriverName: null,
        LicenseType: null,
        DrivingAge: null,
        DriverPhone: null,
        DriverIdNumber: null,
        DriverLicenseValidity: null
      },
      DriverArchive: {
        UserId: null,
        DriverName: null,
        LicenseType: null,
        DrivingAge: null,
        DriverPhone: null,
        DriverIdNumber: null,
        DriverLicenseValidity: null
      },
      licenseTypeList: ["A1", "A2", "B1", "B2", "C1", "C2"],
      //分页相关数据
      tablePage: {
        pageNum: 1, // 第几页
        pageSize: 8, // 每页多少条
        total: 0 // 总记录数
      },
      BasicDataPage: null,
      pageSizes: [4, 8],
    }
  },
  computed: {
    filtedTableData() {
      if (!this.DriverArchive.DriverName && !this.DriverArchive.DrivingAge && !this.DriverArchive.DriverPhone && !this.DriverArchive.LicenseType) {
        return this.getPageList(this.tableData, this.tablePage.pageNum, this.tablePage.pageSize)
      } else {
        return this.getPageList(this.tableData.filter((value) => {
          const nameMatch = !this.DriverArchive.DriverName || value.driverName.includes(this.DriverArchive.DriverName);
          const ageMatch = !this.DriverArchive.DrivingAge || value.drivingAge.toString().includes(this.DriverArchive.DrivingAge);
          const phoneMatch = !this.DriverArchive.DriverPhone || value.driverPhone.includes(this.DriverArchive.DriverPhone);
          const typeMatch = !this.DriverArchive.LicenseType || value.licenseType.includes(this.DriverArchive.LicenseType);

          return nameMatch && ageMatch && phoneMatch && typeMatch;
        }), this.tablePage.pageNum, this.tablePage.pageSize)
      }
    }
  },
  methods: {
    checkAndUpdate(row) {
      this.$router.push({
        path: "/UpgradeDriverArchives",
        query: {
          userId: row.userId,
          driverName: row.driverName,
          licenseType: row.licenseType,
          drivingAge: row.drivingAge,

          driverPhone: row.driverPhone,
          driverIdNumber: row.driverIdNumber,
          driverLicenseValidity: row.driverLicenseValidity,

        }
      });
    },

    DriverArchiveDelete(row) {

      this.$confirm('此操作将删除该驾驶员档案, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        driverArchiveDelete(row.userId).then(res => {
          if (res.data.code == '0') {
            this.$message({
              message: "删除成功",
              duration: 2000,
              showClose: true,
              type: 'success'
            });
            setTimeout(() => {
              location.reload();
            }, 700)
          }
          else {
            this.$message.error(res.data.msg);
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    initTableData() {
      driverPageQuery().then(res => {
        this.tableData = res.data.data
      })
    },
    // 分页
    handlePageChange(currentPage) {
      this.tablePage.pageNum = currentPage
      // 在此刷新数据
      this.BasicDataPage = this.getList(this.tableData, this.tablePage.pageNum, this.tablePage.pageSize)
    },
    handleSizeChange(pageSize) {
      this.tablePage.pageSize = pageSize
      // 在此刷新数据
      this.BasicDataPage = this.getList(this.tableData, this.tablePage.pageNum, this.tablePage.pageSize)
    },
    getList(data, num, size) {
      let list, start, end
      start = (num - 1) * size
      end = start + size
      list = data.filter((item, index) => {
        return index >= start && index < end
      })
      list.forEach((item, index) => {
        item.seq = index + start
      })
      return list
    },
    getPageList(data, num, size) {
      this.tablePage.total = data.length
      this.BasicDataPage = this.getList(data, num, size)
      return this.BasicDataPage
    },
    ToAddDriverArchives() {
      this.$router.push('/Home/AddDriverArchives')
    },
    ToUpgradeDriverArchives() {
      this.$router.push('/UpgradeDriverArchives')
    }
  },
  mounted() {
    this.initTableData();
  }

}
</script>

<style scoped>
.driver-archive-container {
  height: 100%;
  width: 95%;
  margin: 0 auto;
  margin-top: 1%;
  background-color: #fff;
}

.driver-archive-table {
  width: 90%;
  margin: 0px auto;
}

.driver-archive-title {
  font-size: x-large;
  justify-content: left;
  width: 10%;
}

.driver-archive-horizon {
  margin: 0 30px;
  height: 1.5px;
  background-color: #ebeef5;
}

.driver-archive-input {
  margin: 0 10px;
  width: 26%;
}

.driver-archive-header {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: 20px;
  width: 100%;
}

.driver-archive-body {
  width: 100%;
  height: 80%;
  margin: 20px auto;
}

.driver-archive-body-header {
  width: 95%;
  margin: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.driver-archive-footer {
  width: 70%;
  height: 10%;
  margin: 0 auto;
}

.driver-archive-select {
  margin: 0 auto;
  width: 33%;

}

.driver-archive-select-item {
  width: 99%;
}

.driver-archive-button {
  margin: 0 20px;
}

.pagination {
  width: 100%;
  margin: 25px auto;
  /* text-align: right; */
}</style>